//https://leetcode.cn/problems/top-k-frequent-words/

class Solution {
public:
    struct greater
    {
        bool operator() (const pair<string,int>& p,const pair<string,int>& q)
        {
            return p.second>q.second || (p.second==q.second&&p.first<q.first);
        }
    };
    vector<string> topKFrequent(vector<string>& words, int k) 
    {
        map<string,int> count;
        for(auto &e:words)
        {
            count[e]++;
        }  

        vector<pair<string,int>> temp(count.begin(),count.end());
        sort(temp.begin(),temp.end(),greater());

        vector<string> ret;
        for(int i=0;i<k;i++)
        {
            ret.push_back(temp[i].first);
        }

        return ret;
    }
};

//

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    void Inoder(TreeNode* cur,TreeNode*&prev)
	{
		if(!cur)
		return;

		Inoder(cur->left,prev);

		cur->left=prev;
		
		if(prev)
		prev->right=cur;

		prev=cur;

		Inoder(cur->right,prev);

	}
    TreeNode* Convert(TreeNode* pRootOfTree) {
		TreeNode* prev=nullptr;
		Inoder(pRootOfTree,prev);
		TreeNode* head=pRootOfTree;
		while(head&&head->left)
		{
			head=head->left;
		}

		return head;
        
    }
};

//https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5?tpId=13&&tqId=11179&rp=1&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    void Inoder(TreeNode* cur,TreeNode*&prev)
	{
		if(!cur)
		return;

		Inoder(cur->left,prev);

		cur->left=prev;
		
		if(prev)
		prev->right=cur;

		prev=cur;

		Inoder(cur->right,prev);

	}
    TreeNode* Convert(TreeNode* pRootOfTree) {
		TreeNode* prev=nullptr;
		Inoder(pRootOfTree,prev);
		TreeNode* head=pRootOfTree;
		while(head&&head->left)
		{
			head=head->left;
		}

		return head;
        
    }
};
